package com.xinkao.example.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xinkao.example.config.annotation.SysLog;
import com.xinkao.example.config.base.BaseController;
import com.xinkao.example.dao.VerifyCodeDao;
import com.xinkao.example.service.LoginService;
import com.xinkao.example.service.impl.SimpleCharVerifyCodeGenImpl;
import com.xinkao.example.util.CommonUtil;
import com.xinkao.example.util.RedisUtils;
import com.xinkao.example.util.constants.Constants;
import com.xinkao.example.util.constants.ErrorEnum;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: LXF
 * @description: 登录相关Controller
 * @date: 2017/10/24 10:33
 */
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {

	@Autowired
	private LoginService loginService;


	@Autowired
	private SimpleCharVerifyCodeGenImpl verifyCodeGenService;

	@Autowired
	private RedisUtils redisUtils;

	/**
	 * 登录
	 */
	@SysLog(module = "登录模块", operate = "登录")
	@PostMapping("")
	public JSONObject authLogin(@RequestBody JSONObject requestJson) {
		CommonUtil.hasAllRequired(requestJson, "username,password,code,paramDate");
		String validateCode = (String) redisUtils.get(requestJson.getString("paramDate") + requestJson.getString("code").toUpperCase());
		String key = requestJson.getString("paramDate") + requestJson.getString("code").toUpperCase();
		CommonUtil.verifyCode(validateCode);
		return loginService.authLogin(requestJson,key);
	}

	/**
	 * 登出
	 */
	@SysLog(module = "登录模块", operate = "登出")
	@PostMapping("/logout")
	public JSONObject logout(HttpServletRequest request) {
		JSONObject user = getUser();
		if(ObjectUtil.isNotEmpty(user)){
			redisUtils.del(Constants.REDIS_PERMISSION + user.getString("loginName"));
			redisUtils.del(SecurityUtils.getSubject().getPrincipal().toString());
		}
		return loginService.logout();
	}

	/**
	 * @Author ZSX
	 * @Description 获取验证码图片
	 * @Date 2020/12/21 10:30
	 * @Param [response]
	 * @return
	 **/
	@SysLog(module = "登录模块", operate = "获取验证码图片")
	@GetMapping("/getVerificationCode")
	public void verifyCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
		JSONObject requestJson = CommonUtil.request2Json(request);
		CommonUtil.hasAllRequired(requestJson, "paramDate");
		String paramDate = requestJson.getString("paramDate");
		VerifyCodeDao verifyCode = verifyCodeGenService.generate(80, 28);
		String code = verifyCode.getCode();
		boolean flag = redisUtils.set(paramDate+code, code, Constants.VALIDATE_CODE_TIME);
		//设置响应头
		response.setHeader("Pragma", "no-cache");
		//设置响应头
		response.setHeader("Cache-Control", "no-cache");
		//在代理服务器端防止缓冲
		response.setDateHeader("Expires", 0);
		//设置响应内容类型
		response.setContentType("image/jpeg");
		response.getOutputStream().write(verifyCode.getImgBytes());
		response.getOutputStream().flush();
	}

	/**
	 * 用户信息
	 * @param request
	 * @return
	 */
	@SysLog(module = "登录模块", operate = "用户信息")
	@GetMapping("/getUserInfo")
	public JSONObject getUserInfo(HttpServletRequest request){
		JSONObject user = getUser();
		if(ObjectUtil.isEmpty(user)){
			return CommonUtil.errorJson(ErrorEnum.E_20011);
		}
		JSONObject list = loginService.getUserInfo(user);

		//将权限放在redis中
//		List<String> userPermission = loginService.getUserPermission(user.getString("userId"));
//		redisUtils.set(Constants.REDIS_PERMISSION+user.getString("loginName"), userPermission);

		return CommonUtil.successJson(list);
	}

}
